Ext
		.define(
				'Moodle.controller.Aplication',
				{
					extend : 'Ext.app.Controller',
					config : {
						refs : {
							loginView : "loginview",
							courseView : "courseview"
						},
						control : {
							'#ingresarBtn' : {
								tap : 'onIngregarLogin'
							},
							'#refreshCourseBtn' : {
								tap : 'onRefreshCourse'
							},
							'#courseList' : {
								disclose : 'onCourseListDisclose'
							},

						}
					},					
					slideLeftTransition : {
						type : 'slide',
						direction : 'left'
					},
					slideRightTransition : {
						type : 'slide',
						direction : 'right'
					},
					maskedform : {
						xtype : 'loadmask',
						message : 'Por favor espere... '
								+ '<hr>Procesando Operación'
								+ '<code>...</code>'
					},
					onCourseListDisclose : function() {
						alert('asdasd');
					},
					onRefreshCourse : function() {
						var userStore = Ext.getStore("User");
						Ext.Viewport.setMasked(this.maskedform);
						
						 Ext.Ajax.request({
					            url: 'http://10.114.70.171/moodle/webservice/rest/server.php',													            
					            method: 'post',
					            scope: this,
					            params: {
					                wstoken: userStore.data.items[0].data.token,
					                moodlewsrestformat: 'json',
					                wsfunction: 'moodle_user_get_users_by_id',
					                'userids[0]': userStore.data.items[0].data.userid
					            },
							success: function(response, opts) 
							{															   
								
							   var objStudent = Ext.decode(response.responseText);	
							   
							   if (objStudent.errorcode)
								   {
									   this.tokenRefresh();
								   }   
							   else {
									   var courseStore = Ext.getStore("Course");
									   var enrolledcourses = objStudent[0].enrolledcourses;
									   
									   courseStore.add(enrolledcourses);
									   
									   courseStore.each(function(store){																   
										   store.save();						   					   
									   });
									   												  
									   courseStore.sync();	
									   Ext.Viewport.setMasked(false);
							   }
							   
							}
					        });
						 
						 
						 //Contenido de los Cursos
						 Ext.Ajax.request({
					            url: 'http://10.114.70.171/moodle/webservice/rest/server.php',													            
					            method: 'post',
					            scope: this,
					            params: {
					                wstoken: userStore.data.items[0].data.token,
					                moodlewsrestformat: 'json',
					                wsfunction: 'core_course_get_contents',
					                'courseid': 4
					            },
							success: function(response, opts) 
							{	
							   var course = Ext.decode(response.responseText);
							}
					        });	
						 
					},
					tokenRefresh: function(){
						var userStore = Ext.getStore("User");						
						Ext.Viewport.setMasked(this.maskedform);
						
						Ext.Ajax.request({
							url: 'http://10.114.70.171/moodle/login/token.php',
							method: 'post',
							scope: this,
							params: {
					                username: userStore.data.items[0].data.username,
					                password: userStore.data.items[0].data.password,
					                service: 'moodle_mobile_app',
					                jsonp: 1              
						            },
							 success: function(response, opts) 
							 {	 
								  var obj = Ext.decode(response.responseText); 
								  var records = userStore.data.items[0];
								   records[0].data.token = obj.token;
						           records[0].save();						   					   
								   userStore.sync();
								   
							 }
						});
						
					} ,
					onIngregarLogin : function() {
						
						var loginview = this.getLoginView();
						var userStore = Ext.getStore("User");						
						Ext.Viewport.setMasked(this.maskedform);					
						
						
								Ext.Ajax.request({
								url: 'http://10.114.70.171/moodle/login/token.php',
								method: 'post',
								scope: this,
								params: {
						                username: loginview.getValues().user,
						                password: loginview.getValues().password,
						                service: 'moodle_mobile_app',
						                jsonp: 1              
							            },
								 success: function(response, opts) 
								 {	 
								   var obj = Ext.decode(response.responseText);  
								   var pass = opts.params.password;
											   Ext.Ajax.request({
										            url: 'http://10.114.70.171/moodle/webservice/rest/server.php',										            
										            method: 'post',
										            scope: this,
										            params: 
										            {
										                wstoken: obj.token,
										                moodlewsrestformat: 'json',
										                wsfunction: 'moodle_webservice_get_siteinfo'
										            },
												success: function(response, opts) {												   
													   var obj = Ext.decode(response.responseText);
													
													   var records = userStore.add(obj);
													   records[0].data.token = opts.params.wstoken;
													   records[0].data.password = pass;
													  
											           records[0].save();						   					   
													   userStore.sync();
													  													   
													   Ext.Viewport.animateActiveItem(this.getCourseView(),
																this.slideLeftTransition);
													   
													  													   	 
														 Ext.Ajax.request({
													            url: 'http://10.114.70.171/moodle/webservice/rest/server.php',													            
													            method: 'post',
													            scope: this,
													            params: {
													                wstoken: records[0].data.token,
													                moodlewsrestformat: 'json',
													                wsfunction: 'moodle_user_get_users_by_id',
													                'userids[0]': records[0].data.userid
													            },
															success: function(response, opts) 
															{															   
																
															   var objStudent = Ext.decode(response.responseText);		
															   var courseStore = Ext.getStore("Course");
															   var enrolledcourses = objStudent[0].enrolledcourses;
															   
															   courseStore.add(enrolledcourses);
															   
															   courseStore.each(function(store){																   
																   store.save();						   					   
															   });
															   												  
															   courseStore.sync();	
															   Ext.Viewport.setMasked(false);
															   
															}
													        });
												   
												}
										        });
						  
						 },
						 failure: function(response, opts) {
						   console.log('server-side failure with status code ' + response.status);
						 }
					});
						
						
						

					}
				});
